Declare Sub STARTCAPT (byval ID as integer)

type CreateCaptureWindow_t as function ( _
byval title   as zstring ptr, _
byval flag    as integer, _
byval x       as integer, byval y as integer, _
byval w       as integer, byval h as integer, _
byval hParent as integer, _
byval id      as integer) as integer

Type GetDriverDescription_t As Function ( _
  Byval id       As Integer, _
  Byval lpszName As Zstring Ptr, _
  Byval cbName   As Integer, _
  Byval lpszVer  As Zstring Ptr, _
  Byval cbVer    As Integer) As Integer

dim shared as CreateCaptureWindow_t CreateCaptureWindow
Dim Shared As GetDriverDescription_t GetDriverDescription
dim shared as any ptr hDLL
dim shared as integer capwidth,capheight

Sub _init() constructor
  hDll=DylibLoad("avicap32")
  If (hDll<>0) Then
    CreateCaptureWindow =DylibSymbol(hDLL,"capCreateCaptureWindowA" )
    GetDriverDescription=DylibSymbol(hDLL,"capGetDriverDescriptionA")
  Else
    print #99, "error: can't load [avicap32:DLL] !"
  End If
End Sub

Sub _exit() destructor
  If hDLL<>0 Then DylibFree(hDll)
End Sub

#define CAP_STYLE WS_CHILD 'Or WS_VISIBLE
#define CAP_BASE 1024

enum CAP_MESSAGES
  SET_ERROR_PROC       = CAP_BASE +  2
  SET_FRAME_PROC       = CAP_BASE +  5
  SET_VIDEOSTREAM_PROC = CAP_BASE +  6  
  SET_WAVESTREAM_PROC  = CAP_BASE +  7
  
  DRIVER_CONNECT       = CAP_BASE + 10
  DRIVER_DISCONNECT    = CAP_BASE + 11
  DRIVER_GET_NAME      = CAP_BASE + 12
  DRIVER_GET_VERSION   = CAP_BASE + 13
  DRIVER_GET_CAPS      = CAP_BASE + 14
  
  SET_AUDIOFORMAT      = CAP_BASE + 35
  GET_AUDIOFORMAT      = CAP_BASE + 36
  
  DLG_VIDEOFORMAT      = CAP_BASE + 41
  DLG_VIDEOSOURCE      = CAP_BASE + 42
  DLG_VIDEODISPLAY     = CAP_BASE + 43
  GET_VIDEOFORMAT      = CAP_BASE + 44
  SET_VIDEOFORMAT      = CAP_BASE + 45
  DLG_VIDEOCOMPRESSION = CAP_BASE + 46
  
  SET_PREVIEW          = CAP_BASE + 50
  SET_OVERLAY          = CAP_BASE + 51
  SET_SCALE            = CAP_BASE + 53
  GET_STATUS           = CAP_BASE + 54
  SET_SCROLL           = CAP_BASE + 55
  
  GRAB_FRAME           = CAP_BASE + 60
  GRAB_FRAME_NOSTOP    = CAP_BASE + 61
  CAP_SEQUENCE         = CAP_BASE + 62
  CAP_SEQUENCE_NOFILE  = CAP_BASE + 63
  SET_SEQUENCE_SETUP   = CAP_BASE + 64
  GET_SEQUENCE_SETUP   = CAP_BASE + 65
  CAP_STOP             = CAP_BASE + 68
end enum

type CAPTUREPARMS
  As Integer dwRequestMicroSecPerFrame
  As Integer fMakeUserHitOKToCapture
  As Integer wPercentDropForError
  As Integer fYield
  As Integer dwIndexSize
  As Integer wChunkGranularity
  As Integer fUsingDOSMemory
  As Integer wNumVideoRequested
  As Integer fCaptureAudio
  As Integer wNumAudioRequested
  As Integer vKeyAbort
  As Integer fAbortLeftMouse
  As Integer fAbortRightMouse
  As Integer fLimitEnabled
  As Integer wTimeLimit
  As Integer fMCIControl
  As Integer fStepMCIDevice
  As Integer dwMCIStartTime
  As Integer dwMCIStopTime
  As Integer fStepCaptureAt2x
  As Integer wStepCaptureAverageFrames
  As Integer dwAudioBufferSize
  As Integer fDisableWriteCache
  As Integer AVStreamMaster
End Type

type BITMAPINFOHEADER_t
  as integer biSize
  as integer biWidth
  as integer biHeight
  as short   biPlanes
  as short   biBitCount
  as integer biCompression
  as integer biSizeImage
  as integer biXPelsPerMeter
  as integer biYPelsPerMeter
  as integer biClrUsed
  as integer biClrImportant
end type

type POINTAPI
  as integer x,y
end type
type CAPSTATUS
  as integer ImageWidth
  as integer ImageHeight
  as integer LiveWindow
  as integer OverlayWindow
  as integer Scale
  as POINTAPI ptScroll
  as integer UsingDefaultPalette
  as integer  AudioHardware
  as integer  CapFileExists
  as integer  CurrentVideoFrame
  as integer  CurrentVideoFramesDropped
  as integer  CurrentWaveSamples
  as integer  CurrentTimeElapsedMS
  as integer  hPalCurrent
  as integer  CapturingNow
  as integer  dwReturn
  as integer  wNumVideoAllocated
  as integer  wNumAudioAllocated
end type
enum HEADER_FLAGS
  DONE     =  1
  PREPARED =  2
  INQUEUE  =  4
  KEYFRAME =  8
  VALID    = 15
end enum
type RGB24
  as ubyte r,g,b
end type
type RGB32 field=1
  as ubyte b,g,r,a
end type
type VIDEOHDR
  as ubyte ptr lpData
  as integer BufferSize
  as integer BytesUsed
  as integer TimeCaptured
  as any ptr lpUserData
  as integer Flag
  as any ptr Reserved(0 to 3)
end type

function ErrorProc( _
  byval hWin   as integer, _
  byval id     as integer, _
  byval lpText as zstring ptr) as integer
  windowtitle "error callback"
  ? "error proc !" & *lpText
  return 1
end function